//
//  XYuser.m
//  MovieDate
//
//  Created by ann on 16/3/25.
//  Copyright © 2016年 MovieDate. All rights reserved.
//

#import "XYuser.h"

@implementation XYuser
-(XYuser *)init{
    if ([super init]) {
    self.labels = [NSMutableArray array];
    }
    return self;
}
- (void)setActiveTime:(NSString *)activeTime
{
    if (!activeTime || [activeTime rangeOfString:@"null"].length > 0) {
        _activeTimeLast = [NSString stringWithFormat:@"%d小时", rand() % 23 + 1];
        return;
    }
    _activeTime = activeTime;
    
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    
    //创建了两个日期对象
    NSDate *date1 = [dateFormatter dateFromString:activeTime.length>=16?[activeTime substringToIndex:16]:@""];
    NSDate *date=[NSDate date];
    NSString *curdate=[dateFormatter stringFromDate:date];
    NSDate *date2 = [dateFormatter dateFromString:curdate];
    
    //取两个日期对象的时间间隔：
    //这里的NSTimeInterval 并不是对象，是基本型，其实是double类型，是由c定义的:typedef double NSTimeInterval;
    NSTimeInterval time=[date2 timeIntervalSinceDate:date1];
    
    int days=((int)time)/(3600*24);
    int hours=((int)time)%(3600*24)/3600;
    NSString *dateContent = [[NSString alloc] initWithFormat:@"%@%i小时前", days>0?[NSString stringWithFormat:@"%i天", days]:@"", hours>=0?hours:-hours];
    _activeTimeLast = dateContent;
}

- (void)setDistance:(NSString *)distance
{
    if (!distance || [distance intValue] == 0) {
        _distance = [NSString stringWithFormat:@"%dkm", rand() % 9 + 1];
    } else {
        _distance = [NSString stringWithFormat:@"%0.2fkm", [distance floatValue] / 1000];
    }
}

- (void)setAddress:(NSString *)address
{
    if (!address || [address rangeOfString:@"null"].length > 0) {
        _address = [User defaultUser].pStrLocationAddress;
    } else {
        _address = address;
    }
}

@end
